home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / TGCBOR20.ARJ / INTROPAK.COM / TWDEMO.C < prev    next >
C/C++ Source or Header  |  1991-08-28  |  17KB  |  508 lines

  1.  
  2.   /* ------------------------------------------------------------------- */
  3.   /*  TWDEMO.PAS                                                         */
  4.   /*  Copyright 1991 TEGL SYSTEMS CORPORATION, All rights reserved.      */
  5.   /* ------------------------------------------------------------------- */
  6.  
  7. #include "teglsys.h"
  8. #include "math.h"
  9.  
  10.   /* -- Global variables  */
  11.  
  12.   int TECHNA_FONT;
  13.   int TALL_FONT;
  14.  
  15.   imagestkptr  mainmenu;   /* -- the main bar menu is only an ordinary frame */
  16.                  /* -- with option menu click areas placed on it.  */
  17.   fontptr      menufont;   /* -- the font to use with all menus. Set once so  */
  18.                  /* -- we can make the program look better on  */
  19.                  /* -- a variety of video displays.  */
  20.   optionmptr   fileom;
  21.   optionmptr   devicesom;
  22.   optionmptr   dialogom;
  23.   optionmptr   worldom;
  24.  
  25.   /* -- restores all the frames to being active and close the error  */
  26.   /* -- message window.  */
  27.  
  28.  
  29. unsigned errorclose(imagestkptr  ifs, msclickptr   ms)
  30.   {
  31.     resetframeactive(stackptr,TRUE);
  32.     twclose(findwinframe(ifs));
  33.     return 0;
  34.   }
  35.  
  36.   /* -- Display an error message and disable all other frames until  */
  37.   /* -- the OK button is pressed.  */
  38.  
  39.  
  40. void         sayerror(char *       s)
  41.   { winframeptr  wf;
  42.  
  43.       /*  resetframeactive sets the activity of all the frames from  */
  44.       /*  the one passed (here the topmost) to the bottom of the stack. */
  45.       /*  In this case all frames become inactive then we create one  */
  46.       /*  active frame (the error message) that must be delt with, before */
  47.       /*  processing can continue.  */
  48.     resetframeactive(stackptr,FALSE);   /* -- disable everything  */
  49.     twdinit(&wf,0,(getmaxy() / 2) - 35,getmaxx(),(getmaxy() / 2) + 35);
  50.       twsetheader(wf,"ERROR");   /* -- set the header  */
  51.       twsetmaximize(wf,FALSE);   /* -- disable MIN/MAX buttons  */
  52.     /* -- add a button that will acknowlege the error  */
  53.       twdaddbutton(wf,getmaxx() / 2 - 20,25,"OK",errorclose);
  54.       twsetcloseevent(wf,errorclose);   /* -- space bar menu CLOSE  */
  55.     twdrawwindowframe(wf);   /* -- finally draw the window  */
  56.       /* -- display the message.  */
  57.     prepareforupdate(wf->ifs);   /* -- going to write to the window  */
  58.       settextjustify(CENTER_TEXT,TOP_TEXT);
  59.       setcolor(BLACK);
  60.       outtextxy((getmaxx() / 2) - (wf->thickness * 2),5,s);
  61.     commitupdate();   /* -- finished writing to the window  */
  62.   }
  63.   /* --------------------------------------------------------- */
  64.   /* -- These are some global variable for our dialog window to access  */
  65.  
  66.  
  67.     winframeptr  wf            = NULL;
  68.     char         strtd[21]     = "This is a string";
  69.     unsigned char  chkbox        = TRUE;
  70.     int          radio         = 2;
  71.  
  72.   /* -- Note that closing a dialog calls twdClose not twClose, it must */
  73.   /* -- first dispose of the list of dialog entries before the window  */
  74.  
  75.  
  76. unsigned dialogclose(imagestkptr  ifs, msclickptr   ms)
  77.   {
  78.     wf = NULL;
  79.     return twdclose(ifs,ms);
  80.  
  81.   }
  82.  
  83.   /* -- Opens up a simple dialog window.  */
  84.  
  85.  
  86. unsigned opendialogdemo(imagestkptr  ifs, msclickptr   ms)
  87.   { imagestkptr  tempifs;
  88.  
  89.     if (wf != NULL)   /* -- only one allowed.  */
  90.       {
  91.     sayerror("The dialog demo is already running.");
  92.     return 0;
  93.       }
  94.     twdinit(&wf,100,100,400,300);
  95.       twsetheader(wf,"Simple Dialogue");
  96.  
  97.       twdaddlabel(wf,10,10,"Labels go anywhere");
  98.     /* -- input lines are string items, the last parameter is the  */
  99.     /* -- length of the string.  */
  100.       twdaddinputline(wf,10,30,"Edit this ",strtd,20);
  101.     /* -- check boxes are boolean values  */
  102.       twdaddcheckbox(wf,10,50,"a check box",&chkbox);
  103.     /* -- radio buttons all access the same integer value. Their  */
  104.     /* -- order is important. The first one will be one the  */
  105.     /* -- second two, etc. Groups of radio buttons must be seperated  */
  106.     /* -- by either some other dialog item or a label, if nothing is  */
  107.     /* -- required use an empty label.  */
  108.       twdaddradiobutton(wf,10,70,"a radio button (1)",&radio);
  109.       twdaddradiobutton(wf,10,90,"a radio button (2)",&radio);
  110.       twdaddradiobutton(wf,10,110,"a radio button (3)",&radio);
  111.     /* -- Buttons are associated with events, here the OK button does  */
  112.     /* -- nothing, but the cancel button closes the dialog.  */
  113.       twdaddbutton(wf,50,150,"OK",nilunitproc);
  114.       twdaddbutton(wf,180,150,"CANCEL",dialogclose);
  115.       twsetcloseevent(wf,dialogclose);   /* -- the space bar menu  */
  116.  
  117.       /* -- Note that the window is only drawn AFTER ALL THE DIALOG ITEMS  */
  118.       /* -- HAVE BEEN SET.  */
  119.     twdrawwindowframe(wf);
  120.     return 0;
  121.   }
  122.  
  123.   /* -- The key to using scaled text is by setting the usercharsize  */
  124.   /* -- with the ratio of the working area to the screen size  */
  125.  
  126.  
  127. unsigned worldtextredraw(imagestkptr  ifs, msclickptr   ms)
  128.   { winframeptr  wf;
  129.  
  130.     wf = findwinframe(ifs);
  131.     twselect(wf);
  132.     twwdefineworld(wf,0,0,1000,1000);
  133.     prepareforupdate(ifs);
  134.     settextjustify(LEFT_TEXT,TOP_TEXT);
  135.     settextstyle(TECHNA_FONT,HORIZ_DIR,3);
  136.     setusercharsize(wf->wx2 - wf->wx1,getmaxx() / 2,wf->wy2 - wf->wy1,getmaxy() / 2);
  137.     twwouttextxy(wf,10,10,"Scaled text");
  138.     settextstyle(TALL_FONT,HORIZ_DIR,5);
  139.     setusercharsize(wf->wx2 - wf->wx1,getmaxx() / 2,wf->wy2 - wf->wy1,getmaxy() / 2);
  140.     twwouttextxy(wf,10,500,"Using Techna & Tall Font");
  141.     commitupdate();
  142.     return 0;
  143.   }
  144.  
  145.  
  146. unsigned openworldtextdemo(imagestkptr  ifs, msclickptr   ms)
  147.   { winframeptr  wf;
  148.  
  149.     twinit(&wf,100,100,300,250);
  150.       twsetheader(wf,"Scaling text");
  151.       twsetredraw(wf,worldtextredraw);
  152.     twdrawwindowframe(wf);
  153.     return 0;
  154.   }
  155.  
  156.  
  157.   /* --------------------------------------------------------- */
  158.   /* -- Bar graph demo illustrates how to use the world coordinates  */
  159.   /* -- to fit data into any sized window.  */
  160.   /* --------------------------------------------------------- */
  161.  
  162. #define maxbars 10
  163. typedef struct bardef { int   x1, y1, x2, y2, color; } bardef;
  164. bardef       bars[maxbars]
  165.  
  166.   = {{-99,80,-81,0,BLUE},
  167.     {-80,70,-61,0,BLUE},
  168.     {-60,20,-41,0,BLUE},
  169.     {-40,0,-21,-40,RED},
  170.     {-20,0,-1,-99,RED},
  171.     {1,0,20,-67,RED},
  172.     {21,8,40,0,GREEN},
  173.     {41,20,60,0,YELLOW},
  174.     {61,75,80,0,MAGENTA},
  175.     {81,50,99,0,BLUE}};
  176.  
  177.  
  178.  
  179.  
  180.  
  181. unsigned worldbarredraw(imagestkptr ifs, msclickptr ms)
  182.   { winframeptr  wf;
  183.       int      i;
  184.  
  185.     wf = findwinframe(ifs);
  186.     twwdefineworld(wf,-100,100,100,-100);
  187.     twwline(wf,-100,0,100,0);
  188.     for (i = 1; i <= maxbars; i++)
  189.       { bardef *     with1 = &bars[i-1];
  190.  
  191.       setfillstyle(SOLID_FILL,with1->color);
  192.       twwbar(wf,with1->x1,with1->y1,with1->x2,with1->y2);
  193.       setcolor(BLACK);
  194.       twwrectangle(wf,with1->x1,with1->y1,with1->x2,with1->y2);
  195.  
  196.        }
  197.  
  198.     setcolor(BLACK);
  199.     return 0;
  200.   }
  201.  
  202.  
  203. unsigned worldsinredraw(imagestkptr  ifs, msclickptr   ms)
  204.   { winframeptr  wf;
  205.     float         t;
  206.  
  207.     float         counter       = 0.05;
  208.     float         range         = 8.0;
  209.  
  210.     wf = findwinframe(ifs);
  211.     twwdefineworld(wf,-(range * 1.2),(range * 1.2),(range * 1.2),-(range * 1.2));
  212.     twwline(wf,-range,0.0,range,0.0);
  213.     twwline(wf,0,-range,0.0,range);
  214.     twwline(wf,-range,range,-range,-range);
  215.     twwline(wf,-range,-range,range,-range);
  216.     setcolor(RED);
  217.     t = -range;
  218.     while (t <= range)
  219.       {
  220.       /*  twwputpixel(wf,t,range * sin(t),red);  */
  221.  
  222.     twwline(wf,t,range * sin(t),t + counter,range * sin(t + counter));
  223.     t = t + counter;
  224.       }
  225.  
  226.     setcolor(BLACK);
  227.     return 0;
  228.   }
  229.  
  230.  
  231. unsigned openworldbardemo(imagestkptr  ifs, msclickptr   ms)
  232.   { winframeptr  wf;
  233.  
  234.     twinit(&wf,100,100,400,400);
  235.       twsetthickness(wf,4);
  236.       twsetwinframecolors(wf,LIGHTGRAY,DARKGRAY);
  237.       twsetheader(wf,"Bar Graph (-100,100,100,-100)");
  238.       twsetredraw(wf,worldbarredraw);
  239.       twsetwindowstyle(wf,stdbox);
  240.     twdrawwindowframe(wf);
  241.     return 0;
  242.   }
  243.  
  244.  
  245.  
  246. unsigned openworldsindemo(imagestkptr  ifs, msclickptr   ms)
  247.   { winframeptr  wf;
  248.  
  249.     twinit(&wf,200,200,400,400);
  250.       twsetthickness(wf,4);
  251.       twsetwinframecolors(wf,LIGHTBLUE,BLUE);
  252.       twsetheader(wf,"Sine Wave");
  253.       twsetredraw(wf,worldsinredraw);
  254.       twsetwindowstyle(wf,bevbox);
  255.     twdrawwindowframe(wf);
  256.     return 0;
  257.   }
  258.  
  259.  
  260. #ifdef WCFLOATLINT
  261.  
  262. unsigned worldredraw(imagestkptr  ifs, msclickptr   ms)
  263.   { winframeptr  wf;
  264.  
  265.     twwdefineworld(wf,-10000,10000,10000,-10000);
  266.     twwline(wf,-10000,10000,10000,-10000);
  267.     twwline(wf,10000,10000,-10000,-10000);
  268.     twwrectangle(wf,-5000,-5000,5000,5000);
  269.  
  270.     setfillstyle(SOLID_FILL,BLUE);
  271.     twwbar(wf,-9000,9000,-5000,5000);
  272.     twwarc(wf,0,0,180,360,5000);
  273.     twwellipse(wf,0,0,180,360,6000,6000);
  274.     setcolor(RED);
  275.     twwcircle(wf,0,0,6000);
  276.   }
  277.  
  278.  
  279. #else
  280.  
  281.  
  282. unsigned worldredraw(imagestkptr  ifs, msclickptr   ms)
  283.   { winframeptr  wf;
  284.  
  285.       /* -- the redrawing event should alway find the window then  */
  286.       /* -- select it. Selecting does a fix up in case the window has  */
  287.       /* -- been move and sets the view port to the working area of the  */
  288.       /* -- window.  */
  289.     wf = findwinframe(ifs);
  290.     twselect(wf);
  291.       /* -- twwDefineWorld sets the coordinate system, this must be set  */
  292.       /* -- after the window has been drawn (i.e. not before twDrawWindowFrame  */
  293.     twwdefineworld(wf,-10.0,10.0,10.0,-10.0);
  294.       /* -- the world coordinate call all mimic the standard graphics calls  */
  295.       /* -- execpt they start with tww, have a winframeptr as the first parameter */
  296.       /* -- and the arguments are real. Graphics functions that do not have   */
  297.       /* -- coordinates have no equivalent. Just use the normal graphics call.  */
  298.     setcolor(BLACK);
  299.     twwline(wf,-10.0,10.0,10.0,-10.0);
  300.     twwline(wf,10.0,10.0,-10.0,-10.0);
  301.     twwrectangle(wf,-5,-5,5,5);
  302.     setfillstyle(SOLID_FILL,BLUE);
  303.     twwbar(wf,-9,9,-5,5);
  304.       /* -- arcs may not work the way you expect, ellipse is better  */
  305.  
  306.     twwarc(wf,0,0,360,180,5.0);
  307.     twwellipse(wf,0,0,180,360,4.0,4.0);
  308.       /* -- here we draw an ellipse then a circle with the same center point  */
  309.       /* -- and radius, as you resize the window you can see the effect on the  */
  310.       /* -- aspect ratio.  */
  311.     twwellipse(wf,0,0,1,360,6.0,6.0);
  312.     setcolor(RED);
  313.     twwcircle(wf,0,0,6.0);
  314.     return 0;
  315.   }
  316.  
  317. #endif
  318.  
  319.  
  320. unsigned openworlddemo(imagestkptr  ifs, msclickptr   ms)
  321. { winframeptr  wf;
  322.  
  323.  
  324.     /* --------- */
  325.   twinit(&wf,100,100,400,300);
  326.     twsetthickness(wf,6);
  327.     twsetwinframecolors(wf,LIGHTGRAY,DARKGRAY);
  328.     twsetheader(wf,"World coordinates");
  329.     twsetredraw(wf,worldredraw);
  330.     twsetwindowstyle(wf,stdbox);
  331.   twdrawwindowframe(wf);
  332.   return 0;
  333. }
  334.  
  335.   /* --------------------------------------------------------- */
  336.   /* -- Local menus in high level windows.  */
  337.   /* -- This just draws the menus, no events are attached.  */
  338.  
  339.  
  340. unsigned openmenudemo(imagestkptr  ifs, msclickptr   ms)
  341.   { winframeptr  wf;
  342.  
  343.     twinit(&wf,50,50,250,250);
  344.     twsetdisplayfont(wf,f8x8bold);
  345.     twsetheader(wf,"LOCAL MENU Demo");
  346.       /* -- note that certain letters are surrounded by tildes, these  */
  347.       /* -- will be underlined when display and the appropriate key will  */
  348.       /* -- be captured.  */
  349.   twmenuitem(wf,"~F~ile",TRUE);
  350.     twsubmenuitem(wf,"~O~pen",TRUE,nilunitproc);
  351.     twsubmenuitem(wf,"~S~ave",TRUE,nilunitproc);
  352.     twsubmenuitem(wf,"Save ~a~s...",TRUE,nilunitproc);
  353.     twsubmenuitem(wf,"-",FALSE,nilunitproc);
  354.  
  355.     twsubmenuitem(wf,"E~x~it",TRUE,twmenucloseevent);
  356.   twmenuitem(wf,"~E~dit ",TRUE);
  357.     twsubmenuitem(wf,"~U~ndo",TRUE,nilunitproc);
  358.     twsubmenuitem(wf,"~S~elect",TRUE,nilunitproc);
  359.   twmenuitem(wf,"~W~indow",TRUE);
  360.     twsubmenuitem(wf,"~T~ile",TRUE,nilunitproc);
  361.     twsubmenuitem(wf,"~S~elect",TRUE,nilunitproc);
  362.   twmenuitem(wf,"~H~elp",TRUE);
  363.     twsubmenuitem(wf,"~C~ontents",TRUE,nilunitproc);
  364.     twsubmenuitem(wf,"~I~ndex",TRUE,nilunitproc);
  365.     twdrawwindowframe(wf);
  366.   return 0;
  367.   }
  368.  
  369.  
  370.   /* --------------------------------------------------------- */
  371.   /* -- Slider action in high level windows. The Window Frame  */
  372.   /* -- maintains variables that are updated after a slider is */
  373.   /* -- moved or a slider end button is pressed. This is           */
  374.   /* -- looked after by some events in TWKERNEL. The user must */
  375.   /* -- still create an event for each slider that will then   */
  376.   /* -- interrogate these variables and take the appropriate   */
  377.   /* -- action.  */
  378.  
  379.  
  380.  
  381.   /* -- this is the up down slider event that is called by the  */
  382.   /* -- kernel after the slider is moved.  */
  383.  
  384.  
  385. unsigned showupdownaction(imagestkptr  ifs, msclickptr   ms)
  386.   { winframeptr  wf;
  387.  
  388.     wf = findwinframe(ifs);
  389.     prepareforupdate(ifs);
  390.     twcrtassign(wf);
  391.     twclear(wf);
  392.     twgotoxy(wf,1,2);
  393.     twprintf(" Up Down Action\n");
  394.     twgotoxy(wf,1,3);
  395.     twprintf(" Slider percent: %i\n",wf->updnslideper);
  396.     twgotoxy(wf,1,4);
  397.     twprintf(" button up     : %i\n",wf->upbuttonpress);
  398.     twgotoxy(wf,1,5);
  399.     twprintf(" button down   : %i\n",wf->dnbuttonpress);
  400.     twgotoxy(wf,1,6);
  401.     twprintf(" Slider Range  : %i\n",wf->updownrange);
  402.     commitupdate();
  403.     return 0;
  404.   }
  405.  
  406.   /* -- this is the left right slider event that is called by the  */
  407.   /* -- kernel after the slider is moved or the  */
  408.  
  409.  
  410. unsigned showleftrightaction(imagestkptr  ifs, msclickptr   ms)
  411.   { winframeptr  wf;
  412.  
  413.     wf = findwinframe(ifs);
  414.     prepareforupdate(ifs);
  415.     twcrtassign(wf);
  416.     twclear(wf);
  417.     twgotoxy(wf,1,2);
  418.     twprintf(" Left Right Action\n");
  419.     twgotoxy(wf,1,3);
  420.     twprintf(" Slider percent: %i\n",wf->lfrtslideper);
  421.     twgotoxy(wf,1,4);
  422.     twprintf(" button left   : %i\n",wf->lfbuttonpress);
  423.     twgotoxy(wf,1,5);
  424.     twprintf(" button right  : %i\n",wf->rtbuttonpress);
  425.     twgotoxy(wf,1,6);
  426.     twprintf(" Slider Range  : %i\n",wf->leftrightrange);
  427.     commitupdate();
  428.     return 0;
  429.   }
  430.  
  431.   /* -- this event is called from a menu selection or a mouse click.  */
  432.   /* -- It ignores the parameters and simply opens up a demo window  */
  433.   /* -- for sliders. The WinFramePtr can be a local variable because  */
  434.   /* -- it can be located in events that are attached to it (sliders  */
  435.   /* -- here) by using findwinframe and the passed imagestkptr.  */
  436.  
  437.  
  438. unsigned opensliderdemo(imagestkptr  ifs, msclickptr   ms)
  439.   { winframeptr  wf;
  440.  
  441.     twinit(&wf,100,100,450,300);   /* -- allocates memory and set size  */
  442.     twsetupdownrange(wf,2000,100);
  443.     twsetleftrightrange(wf,2000,100);
  444.     twsetfont(wf,f8x12bol);   /* -- the font to use  */
  445.     twsetheader(wf,"A slider action example.");   /* -- header on  */
  446.     twsetupdownslider(wf,TRUE);   /* -- up down slider on  */
  447.     twsetleftrightslider(wf,TRUE);   /* -- left right slider on  */
  448.     twsetupdownevent(wf,showupdownaction);   /* -- attach above event  */
  449.     twsetleftrightevent(wf,showleftrightaction);   /* -- ditto  */
  450.     twdrawwindowframe(wf);   /* -- finally draw it.  */
  451.     return 0;
  452.   }
  453.  
  454.  
  455.  
  456. void main(void)
  457. {
  458.  
  459.  
  460.     /* -- simple start up, minimal normal heap, TWCOMMON  */
  461.   tweasystart();
  462.   TECHNA_FONT = installuserfont("TECHNA.CHR");
  463.   TALL_FONT   = installuserfont("TALL.CHR");
  464.   setkeyboardmouse(FALSE);
  465.     /* -- MaxWindowSize is the resolution of the window manager. Try  */
  466.     /* -- changing this value between 32000 - 128000, larger values  */
  467.     /* -- are better but may cause program failure because of heap size  */
  468.     /* MaxWindowSize := 64000;  */
  469.  
  470.     /* -- set the font to use in window headers, TWCOMMON  */
  471.   twsetheaderfont(f8x12bol);
  472.  
  473.   menufont = f8x12bol;   /* -- set the font to use on the menu  */
  474.  
  475.   fileom = createoptionmenu(menufont);
  476.     defineoptions(fileom,"-",FALSE,nilunitproc);
  477.     defineoptions(fileom," E~x~it ",TRUE,twexitoption);
  478.   devicesom = createoptionmenu(menufont);
  479.     defineoptions(devicesom,"~M~enus",TRUE,openmenudemo);
  480.     defineoptions(devicesom,"~S~liders",TRUE,opensliderdemo);
  481.  
  482.   dialogom = createoptionmenu(menufont);
  483.     defineoptions(dialogom,"~S~imple",TRUE,opendialogdemo);
  484.   worldom = createoptionmenu(menufont);
  485.     defineoptions(worldom,"~S~ample",TRUE,openworlddemo);
  486.     defineoptions(worldom,"~B~ar Graph ",TRUE,openworldbardemo);
  487.     defineoptions(worldom,"S~i~ne wave",TRUE,openworldsindemo);
  488.     defineoptions(worldom,"~T~ext (scaled)",TRUE,openworldtextdemo);
  489.  
  490.     /* -- we create the bar menu last an attach all the option menus to  */
  491.     /* -- it, this is the only order that will work.  */
  492.  
  493.   setteglfont(menufont);   /* -- bar menu uses the current font  */
  494.   createbarmenu(0,0,getmaxx());
  495.   mainmenu = stackptr;   /* -- just another frame  */
  496.     outbaroption(" ~F~ile ",fileom);
  497.     outbaroption(" ~D~evices ",devicesom);
  498.     outbaroption(" Dia~l~ogues ",dialogom);
  499.     outbaroption(" ~W~orld ",worldom);
  500.  
  501.   setautorotate(TRUE);   /* -- windows rotate to the top automatically  */
  502.   teglsupervisor();   /* -- do not adjust your set, the supervisor  */
  503.                                 /* -- is in control!  */
  504. }
  505.  
  506.  
  507.  
  508.